package com.jrx.anytxn.param.service.limit.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.limit.CreditLimitMappingReq;
import com.jrx.anytxn.param.dto.limit.CreditLimitMappingRes;
import com.jrx.anytxn.param.entity.PrCreditLimitMapping;
import com.jrx.anytxn.param.mapper.ext.ExtPrCreditLimitMappingMapper;
import com.jrx.anytxn.param.service.limit.ICreditLimitMappingService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by yf
 * @Classname CreditLimitMappingServiceImpl
 * @Description 额度节点映射接口实现
 * @Date 2019/7/17 0017 下午 14:58
 */
@Service
public class CreditLimitMappingServiceImpl implements ICreditLimitMappingService {
    private static final Logger logger = LoggerFactory.getLogger(CreditLimitMappingServiceImpl.class);

    @Resource
    private ExtPrCreditLimitMappingMapper extPrCreditLimitMappingMapper;

    @Autowired
    private ISegmentService segmentService;


    @Override
    public TxnPage<CreditLimitMappingRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<CreditLimitMappingRes> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrCreditLimitMapping> creditLimitMappingResList = extPrCreditLimitMappingMapper.selectLimitMappingByTenId(tenantId);
        if (creditLimitMappingResList.isEmpty()) {
            logger.error("查询的额度节点映射列表为空");
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<CreditLimitMappingRes> res = BeanMapping.copyList(creditLimitMappingResList, CreditLimitMappingRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public CreditLimitMappingRes findByTenIdAndChannelAndProId(String tenantId, String channel, String productId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(channel) || StringUtils.isBlank(productId)) {
            logger.error("查询额度节点映射时租户ID或渠道或产品ID参数为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrCreditLimitMapping prCreditLimitMapping = extPrCreditLimitMappingMapper.selectByTenIdAndProIdAndChannel(tenantId, channel, productId);
        return BeanMapping.copy(prCreditLimitMapping, CreditLimitMappingRes.class);
    }

    @Caching(evict={@CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#tenantId+'_'+#channel"),
            @CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#tenantId+'_'+#channel+'_'+#productId")})
    @Override
    public Boolean removeByTenIdAndChannelAndProId(String tenantId, String channel, String productId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(channel) || StringUtils.isBlank(productId)) {
            logger.error("删除额度节点映射时租户ID或渠道或产品ID参数为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int a = extPrCreditLimitMappingMapper.deleteByTenIdAndChannelAndProId(tenantId, channel, productId);
        return a > 0;
    }

    @Caching(evict={@CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#creditLimitMappingReq.tenantId+'_'+#creditLimitMappingReq.channel"),
            @CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#creditLimitMappingReq.tenantId+'_'+#creditLimitMappingReq.channel+'_'+#creditLimitMappingReq.productId")})
    @Override
    public CreditLimitMappingRes addLimitMapping(CreditLimitMappingReq creditLimitMappingReq) throws TxnException {
        PrCreditLimitMapping prCreditLimitMapping = BeanMapping.copy(creditLimitMappingReq, PrCreditLimitMapping.class);
        /*根据索引查看是否存在此信息*/
        PrCreditLimitMapping limitMapping = extPrCreditLimitMappingMapper.selectByTenIdAndProIdAndChannel(prCreditLimitMapping.getTenantId(), prCreditLimitMapping.getChannel(), prCreditLimitMapping.getProductId());
        if (limitMapping != null) {
            logger.error("增加的额度节点映射表信息已存在");
            throw new TxnException(TxnRespCode.DATA_EXISTS, TxnRespCode.DATA_EXISTS.getMsg());
        }
        try {
            prCreditLimitMapping.setId(segmentService.getId(Constant.PR_CREDIT_LIMIT_MAPPING));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prCreditLimitMapping.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrCreditLimitMappingMapper.insertSelective(prCreditLimitMapping);
        return BeanMapping.copy(prCreditLimitMapping, CreditLimitMappingRes.class);
    }

    @Caching(evict={@CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#creditLimitMappingReq.tenantId+'_'+#creditLimitMappingReq.channel"),
            @CacheEvict(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#creditLimitMappingReq.tenantId+'_'+#creditLimitMappingReq.channel+'_'+#creditLimitMappingReq.productId")})
    @Override
    public CreditLimitMappingRes modifyLimitMapping(CreditLimitMappingReq creditLimitMappingReq) {
        PrCreditLimitMapping limitMapping = BeanMapping.copy(creditLimitMappingReq, PrCreditLimitMapping.class);
        extPrCreditLimitMappingMapper.updateByTenIdAndChannelAndProId(limitMapping);
        return BeanMapping.copy(limitMapping, CreditLimitMappingRes.class);
    }

    @Cacheable(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#tenantId+'_'+#channel")
    @Override
    public List<PrCreditLimitMapping> getLimitMappingByChannelAndOrgId(String organizationId, String channel, String tenantId) {
        return  extPrCreditLimitMappingMapper.selectLimitMappingAndProductByChannelAndOrganizationId(organizationId, channel, tenantId);
      
    }

    @Cacheable(value = "CREDIT_LIMIT",key = "'CreditLimitMapping_'+#tenantId+'_'+#channel+'_'+#productId")
    @Override
    public String getLimitId(String organizationId, String channel, String productId, String tenantId) {
        return extPrCreditLimitMappingMapper.selectLimitId(organizationId, channel, productId, tenantId);
    }

    @Override
    public List<String> selectByTenantId(String tenantId) {
        return extPrCreditLimitMappingMapper.selectLimitIdByTenId(tenantId);
    }

    @Override
    public List<PrCreditLimitMapping> selectByTenantIdAndChannel(String organizationId, String channel, String tenantId, String limitId) {
        return extPrCreditLimitMappingMapper.selectLimitListIdAndTenantId(organizationId,channel,tenantId,limitId);
    }

}
